var currentRunningVideoID = -1;
var currentRunningEmbeddedID = -1;
var isTabBeingPaused = false;
var videos = {};
var embeds = {};

window.setTimeout(function () { 

var scr = document.createElement('script');
scr.type="text/javascript";
scr.innerText=" function onYouTubePlayerReady(playerId) { console.log('enters here!'); ytplayer = document.getElementsByTagName('embed')[0]; ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');} function onytplayerStateChange(newState) { if (newState == 1)  { var customEvent = document.createEvent('Event'); customEvent.initEvent('play', true, false); ytplayer.dispatchEvent(customEvent); console.log('event dispatched'); } else if (newState == 2) {var customEvent = document.createEvent('Event'); customEvent.initEvent('pause', true, false); ytplayer.dispatchEvent(customEvent); } }";

document.getElementsByTagName("head")[0].appendChild(scr);

//html5, embeds
videos = document.getElementsByTagName("video");
embeds = document.getElementsByTagName("embed");

var ytPattern = new RegExp("[A-Za-z0-9:/]*\.youtube\.com/[A-Za-z0-9/?=]*");
var ytPattern2 = new RegExp("[A-Za-z0-9:/]*\.ytimg\.com/[A-Za-z0-9/?=]*");
var jsAPIPattern = new RegExp("[A-Za-z0-9:/.?&=]*enablejsapi=1[A-Za-z0-9:/=?]*");

for (var i = 0, video; video = videos[i]; i++) {
	video.setAttribute('videoid', i);
	video.addEventListener('play', playing, false);
	video.addEventListener('pause', pausing, false);
}

for (var i = 0, embed; embed = embeds[i]; i++) {
	var source = embed.src;
	console.log(source);
	if (ytPattern.test(source) || ytPattern2.test(source)) {
		if (!jsAPIPattern.test(source)) {
			embed.src = source + "&enablejsapi=1";
		}
	}
	console.log(embed.src);
	embed.setAttribute('embeddedid', i);
	embed.addEventListener('play', playing, false);
	embed.addEventListener('pause', pausing, false);
}

function playing() {
	console.log("playing");
	currentRunningVideoID = this.getAttribute('videoid');
	currentRunningEmbeddedID = this.getAttribute('embeddedid');
	console.log(currentRunningVideoID + " : " + currentRunningEmbeddedID);
	isTabBeingPaused = true;
	chrome.extension.sendRequest({videoid:currentRunningVideoID, embedid:currentRunningEmbeddedID }, function(response) {});
}

function pausing() {
	console.log("pausing");
	console.log(currentRunningVideoID + " : " + currentRunningEmbeddedID);
	currentRunningVideoID = -1;
	currentRunningEmbeddedID = -1;
}

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {

 if (videos.length != document.getElementsByTagName("video").length || embeds.length != document.getElementsByTagName("embed").length) {
	console.assert(false);
	alert("videos and/or embedded videos not counted properly");
}


 if (request.action == "pauseVideo") {
  for (var i = 0, video; video = videos[i]; i++) {
	if (!isTabBeingPaused || currentRunningVideoID != i) {
		video.pause();
	}
	else {
		isTabBeingPaused = false;
		console.log(isTabBeingPaused);
	}
  }
  for (var i=0, embed; embed = embeds[i]; i++) {
	try {
		if (!isTabBeingPaused || currentRunningEmbeddedID != i) {
			embed.pauseVideo();
		}
		else {
			isTabBeingPaused = false;
			console.log(isTabBeingPaused);
		}
	}
	catch(err) {
        }
  }
 }
 else {
  //no action necessary here!
 }

 sendResponse({}); // Send nothing..

});
}, 0);
